package pack2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main2Frequency {

		public static void main (String[] args) throws Exception {
		      
		       Map<String, Map<String, Integer>>map = new HashMap<String, Map<String, Integer>>();
		       Map<String, Integer>map2 = new TreeMap<String, Integer>();
		       //System.out.println(map);
		       String file = "C:\\Users\\Alket\\codeindigo\\DataTel\\PLS2PLS";
		       
		       Map<String, List<Integer>>map3 = new HashMap<String, List<Integer>>();
		       map = freq(file);
		    
		    
		       print(map, "FinalFile.txt");
		}
		
		public static Map<String, Map<String , Integer>> freq(String inflow)throws Exception{

			Map<String, Map<String , Integer>> map = new HashMap<String, Map<String, Integer>>(); 
			File Folder = new File(inflow);
			File files[];
			files = Folder.listFiles();
            String fullname ;
            String name;
			if (files.length>1){

				for(int i = 0; i < files.length; i++){
					
					Map<String , Integer>map_i = new HashMap<String, Integer>();
					BufferedReader br = new BufferedReader(new FileReader(files[i]));
					fullname = files[i].getName();
					System.out.println(i);
					name = fullname.substring(fullname.indexOf("_")+1, fullname.indexOf(","));
					String line;
					while((line = br.readLine())!= null){
                         // System.out.println(line);
						String[] r = line.split(",");
						String h2 = r[7];
						Integer c = map_i.get(h2);
						if(c==null)
							map_i.put(h2, 1);
						else
							map_i.put(h2, c+1);

					}br.close();
					System.out.println(name+" ----> "+map_i.get(name));
					map.put(name, map_i);
				}
			}
			return map;
		}
		
		public static void print (Map<String, Map<String, Integer>>map , String file) throws Exception{
			
			PrintWriter out = new PrintWriter(new FileWriter(new File(file)));
			
			for (String s : map.keySet()) {
				
				for (String k: map.get(s).keySet()) {
					out.println(s+", "+k+", "+map.get(s).get(k));
				}
			}
			out.close();
		}
		
		public static void load(){
			
		}
}
	

